import axios from 'axios';
import { ElMessage } from 'element-plus';

enum MSGS {
    "操作成功" = 200,
    "密码错误",
    "账号错误",
    "请求异常"
}

// 创建HTTP实例
const $http = axios.create({
    baseURL: "http://162.14.65.171:8080/admin",//后端 王浩成
    // baseURL: "http://172.16.141.206:8081/duomei",//后端 汪泓江
    timeout: 2000,
    headers: {
        "Content-Type": "application/json;charset=utf-8"
    }
})

//请求拦截
$http.interceptors.request.use(config => {
    config.headers = config.headers || {}
    if (localStorage.getItem('token')) {
        config.headers.token = localStorage.getItem('token') || ''
    }

    return config
})

//响应拦截
$http.interceptors.response.use(res => {
    const code: number = res.data.code
    if (code !== 200) {
        ElMessage.error(MSGS[code])
        return Promise.reject(res.data)
    }
    return res.data
}, err => {
    console.log(err)
})

export default $http
